package com.qianxx.view.xrecyclerview;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/15.
 * <p>
 * Adapter 提供添加 View 和 移除 View
 */
public class XRecyclerViewAdapter extends RecyclerView.Adapter {

    /**
     * 布局类型
     * 1、内容
     * 2、加载更多
     */
    private static final int ITEM_TYPE_LOADING = 1000;

    private final RecyclerView.Adapter mAdapter;

    private boolean isLoadMore = true;
    /**
     * FooterView
     */
    private XFooterView mXFooterView;
    /**
     * 点击事件
     */
    private OnItemClickListener mOnItemClickListener;

    public XRecyclerViewAdapter(Context context, RecyclerView.Adapter adapter) {
        mAdapter = adapter;

        mXFooterView = new XFooterView(context);
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        if (isFooterView(holder.getLayoutPosition())) {
            ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();

            if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
                StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
                p.setFullSpan(true);
            }
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) layoutManager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if (isFooterView(position)) {
                        return gridManager.getSpanCount();
                    }
                    return 1;
                }
            });
        }
    }

    private class HeaderBottomHolder extends RecyclerView.ViewHolder {

        HeaderBottomHolder(View itemView) {
            super(itemView);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_LOADING) {
            return new HeaderBottomHolder(mXFooterView);
        }
        return mAdapter.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder.getItemViewType() == ITEM_TYPE_LOADING) {
            return;
        }

        final int realPosition = position;

        mAdapter.onBindViewHolder(holder, position);

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView, realPosition);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onItemLongClick(holder.itemView, realPosition);
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mAdapter.getItemCount() + getFooterViewCount();
    }

    @Override
    public int getItemViewType(int position) {
        // 如果是最后一个 ITEM_TYPE_LOADING ， 其他返回自己本身的 ViewType
        if (isFooterView(position)) {
            return ITEM_TYPE_LOADING;
        } else {
            return mAdapter.getItemViewType(position);
        }
    }

    public void setLoadMore(boolean loadMore) {
        isLoadMore = loadMore;
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        mOnItemClickListener = itemClickListener;
    }

    public XFooterView getXFooterView() {
        return mXFooterView;
    }

    private boolean isFooterView(int position) {
        return position >= getItemCount() - getFooterViewCount();
    }

    private int getFooterViewCount() {
        int count = 0;
        if (isLoadMore) {
            count += 1;
        }
        return count;
    }
}
